package com.soft.servlet;

import com.soft.entity.*;
import com.soft.service.Domain;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author kongz
 * @version 1.0 2021/6/7 20:29
 * @description 结账servlet
 */
@WebServlet(name = "JieZhangServlet", urlPatterns = "/JieZhang")
public class JieZhangServlet extends HttpServlet {
    private Domain domain;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        domain = new Domain();
        if (req.getParameter("shopOrder.username") != null) {
            String buyer = req.getParameter("shopOrder.username");
            if (domain.getUserInfo(buyer) != null) {
                UserInfo ui = domain.getUserInfo(buyer);
                req.setAttribute("Buyer", ui);
                req.getRequestDispatcher("jiezhang.jsp").forward(req, resp);
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        domain = new Domain();

        UserInfo ui = (UserInfo) req.getSession().getAttribute("user");
        String buyer = req.getParameter("shopOrder.username");
        String deliveryMethod = req.getParameter("shopOrder.carry");
        String payMethod = req.getParameter("shopOrder.pay");
        String detail = req.getParameter("shopOrder.bz");

        Order order = new Order();
        order.setBuyer(ui.getId());
        order.setReceiver(buyer);
        order.setPayMethod(payMethod);
        order.setDeliveryMethod(deliveryMethod);
        order.setDetail(detail);

        if (domain.newOrder(order, ui.getId())) {
            List<TrolleyInfo> list = domain.getTrolleyInfoByUser(ui.getId());
            for (TrolleyInfo ti : list) {
                domain.deleteTrolleyInfo(ti.getId());
                List<Order> orders  = domain.getOrderAll();
                OrderInfo orderInfo = new OrderInfo();
                orderInfo.setOrderId(String.valueOf(orders.get(orders.size() - 1).getId()));
                orderInfo.setCount(String.valueOf(ti.getCount()));
                orderInfo.setGoodsId(String.valueOf(ti.getGoodsId()));
                domain.insertOrderInfo(orderInfo);
            }
            updateUserLevel(ui.getId(), req);
            req.getSession().setAttribute("flag", 42);
            resp.sendRedirect("/GouWuCheList");
        } else {
            req.getSession().setAttribute("flag", 43);
            doGet(req, resp);
        }
    }

    private void updateUserLevel(String userId, HttpServletRequest req) {
        domain = new Domain();
        double sum = 0.0;
        List<Order> orderList = domain.getOrderByBuyer(userId);
        for (Order order : orderList) {
            List<OrderInfo> orderInfos = domain.getOrderInfoListByOrderId(order.getId());
            for (OrderInfo oi : orderInfos) {
                Goods goods = domain.getGoodsById(oi.getGoodsId());
                sum += goods.getPrice() * oi.getCount();
            }
        }
        UserInfo ui = domain.getUserInfo(userId);
        if (sum > 10000) {
            ui.setLevel(String.valueOf(UserInfo.LEVEL.LEVEL_DIAMOND_VIP));
            domain.updateUserInfo(ui);
            req.getSession().setAttribute("levelChange", 3);
        } else if (sum > 5000) {
            ui.setLevel(String.valueOf(UserInfo.LEVEL.LEVEL_GOLD_VIP));
            domain.updateUserInfo(ui);
            req.getSession().setAttribute("levelChange", 2);
        } else if (sum > 2000) {
            ui.setLevel(String.valueOf(UserInfo.LEVEL.LEVEL_NORMAL_VIP));
            domain.updateUserInfo(ui);
            req.getSession().setAttribute("levelChange", 1);
        }
        req.getSession().setAttribute("user", ui);
    }
}
